package com.kevin.controller.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kevin.common.api.PageDTO;
import com.kevin.common.api.R;
import com.kevin.service.entity.SysDictType;
import com.kevin.service.service.SysDictTypeService;
import com.kevin.service.vo.SysDictVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户控制器
 *
 * @author wh
 * @date 2023/03/27
 */
@RestController
@RequestMapping("sys/dict/type")
@AllArgsConstructor
@Api(tags = "字典管理")
public class DictTypeController {
    private final SysDictTypeService sysDictTypeService;

    @GetMapping("page")
    @ApiOperation(value = "分页")
    public R<?> page(PageDTO pageDTO, String dictType, String dictName) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return R.ok(new PageInfo<>(sysDictTypeService.list(Wrappers.lambdaUpdate(SysDictType.class)
                .like(StrUtil.isNotBlank(dictType), SysDictType::getDictType, dictType)
                .like(StrUtil.isNotBlank(dictName), SysDictType::getDictName, dictName)
                .orderByAsc(SysDictType::getSort))));
    }

    @GetMapping("list/sql")
    @ApiOperation(value = "动态SQL数据")
    @SaCheckPermission(value = "sys:dict:page")
    public R<PageInfo<SysDictVO.DictData>> listSql(Long id) {
        List<SysDictVO.DictData> list = sysDictTypeService.getDictSql(id);
        return R.ok(new PageInfo<>(list));
    }

    @GetMapping("{id}")
    @ApiOperation(value = "信息")
    @SaCheckPermission(value = "sys:dict:info")
    public R<SysDictType> get(@PathVariable("id") Long id) {
        return R.ok(sysDictTypeService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    @SaCheckPermission(value = "sys:dict:save")
    public R<String> save(@RequestBody @Valid SysDictType vo) {
        Assert.notNull(null, "暂不支持新增");
        sysDictTypeService.save(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission(value = "sys:dict:update")
    public R<String> update(@RequestBody @Valid SysDictType vo) {
        sysDictTypeService.updateById(vo);

        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission(value = "sys:dict:delete")
    public R<String> delete(@RequestBody List<Long> idList) {
        sysDictTypeService.removeBatchByIds(idList);

        return R.ok();
    }

    @GetMapping("all")
    @ApiOperation(value = "全部字典数据")
    public R<List<SysDictVO>> all() {
        List<SysDictVO> dictList = sysDictTypeService.getDictList();

        return R.ok(dictList);
    }

    @GetMapping("refreshTransCache")
    @ApiOperation(value = "刷新字典翻译缓存数据")
    @SaCheckPermission(value = "sys:dict:refreshTransCache")
    public R<String> refreshTransCache() {
        sysDictTypeService.refreshTransCache();
        return R.ok();
    }
}
